package com.houserent.view;

import com.houserent.domain.House;
import com.houserent.service.HouseService;
import com.houserent.utils.Utility;


public class Houseview {
    private boolean loop = true;
    private char key = ' ';
    private HouseService houseService =new HouseService(10);//未添加扩容机制

    //编写modifyHouse(),接受输入的Id,调用houseSerivce的modify方法
    public void modifyHouse(){
        System.out.println("=================修改房屋信息=============");
        System.out.println("请输入您要修改的房屋编号(-1退出): ");
        int modId =Utility.readInt();
        if(modId == -1){
            System.out.println("=================放弃修改房屋信息=================");
            return;
        }
        if(houseService.modifyHouse(modId)){
            System.out.println("=================修改成功=================");
        }
        else
            System.out.println("=================编号不存在，修改房屋信息失败=================");
    }
    //编写searchHouse(),接受输入的Id,调用houseSerivce的search方法
    public void searchHouse(){
        System.out.println("=================查找房屋信息=============");
        System.out.println("请输入您要查询的房屋编号(-1退出): ");
        int searchId =Utility.readInt();
        if(searchId == -1){
            System.out.println("=================放弃查找房屋信息=================");
            return;
        }
        if(houseService.search(searchId)){
            System.out.println("=================查找成功=================");
        }
        else
            System.out.println("=================编号不存在，查找房屋信息失败=================");
    }
    //退出确认
    public void exit(){
        char c =Utility.readConfirmSelection();
        if(c=='Y')
            loop = false;
    }
    //编写delHouse(),接受输入的Id,调用houseSerivce的del方法
    public void delHouse(){
        System.out.println("=================删除房屋信息=============");
        System.out.println("请输入待删除的编号(-1退出): ");
        int delId =Utility.readInt();
        if(delId == -1){
            System.out.println("=================放弃删除房屋信息=================");
            return;
        }
        char choice=Utility.readConfirmSelection();
        if(choice=='Y'){
            if(houseService.del(delId)){
                System.out.println("=================删除房屋信息成功=================");
            }
            else
                System.out.println("=================编号不存在，删除房屋信息失败=================");
        }
        else{
            System.out.println("=================放弃删除房屋信息=================");
        }

    }
    //编写addHouse(),接受输入，创建House对象，调用houseSerivce的add方法
    public void addHouse(){
        System.out.println("=================添加房屋=============");
        System.out.print("姓名: ");
        String name =Utility.readString(8);
        System.out.print("电话: ");
        String phone =Utility.readString(11);
        System.out.print("地址: ");
        String address =Utility.readString(16);
        System.out.print("月租: ");
        int rent =Utility.readInt();
        System.out.print("状态: ");
        String state =Utility.readString(3);
        House newhouse = new House(0, name, phone, address, rent, state);
        if(houseService.add(newhouse))
            System.out.println("================添加房屋成功============");
        else
            System.out.println("================添加房屋失败============");

    }

    //编写listHouse()显示房屋列表
    public void listHouse(){
        System.out.println("=================房屋列表=============");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(已出租/未出租)");
        House[] houses = houseService.list();
        for(int i=0;i< houses.length;i++){
            if(houses[i]==null)
                break;
            System.out.println(houses[i].toString());
        }
        System.out.println("=================房屋列表显示完毕=============");

    }
    public void mainmenu(){
    do{
        System.out.println("\n=================房屋出租系统菜单=============");
        System.out.println("\t\t\t1 新 增 房 源");
        System.out.println("\t\t\t2 查 找 房 源");
        System.out.println("\t\t\t3 删 除 房 屋 信 息");
        System.out.println("\t\t\t4 修 改 房 屋 信 息");
        System.out.println("\t\t\t5 房 屋 列 表");
        System.out.println("\t\t\t6 退      出");
        System.out.println("请输入您的选择(1~6):");
        key= Utility.readChar();
        switch(key){
            case '1':
                addHouse();
                break;
            case '2':
                searchHouse();
                break;
            case '3':
                delHouse();
                break;
            case '4':
                modifyHouse();
                break;
            case '5':
                listHouse();
                break;
            case '6':
                exit();
                break;
        }

    }while(loop);
    }
}
